
function postHtml(r){
	var unquiefrom=Math.random();
	$.cookie('unquiefrom', unquiefrom);
	return '<form method="post" id="postFrom">'
    + '<lable id="htmlUnquie">'
    + unquieHtml()
    + '</lable><lable>'
    + lorelang['post_textarea_content']
    + '&nbsp;:&nbsp;<br /><textarea name="content" rows="5" cols="50"></textarea></lable><br />'
    + '<input type="button" onclick="postData(this)" style="float:right;" value="'
    + lorelang['post_button_submit']
    + '" />'
    + '<input type="hidden" name="unquiefrom" id="unquiefrom" value="' + unquiefrom + '" />'
	+ (r?'<input type="hidden" name="r" value="' + r + '" />':'')
    + '</form>';
}
function unquieHtml(){
	var unquie = $.cookie('loreswap_unquie');
	htmls = lorelang['post_input_unquie']
    + '&nbsp;:&nbsp;<br /><input type="text" id="unquie" name="userName" size="40" value="'
	+ (unquie?unquie:'')
	+ '" />';
	if ( unquie ){
		htmls += '<input type="button" onclick="delUnquie()" value="'
		+ lorelang['post_input_unquie_clear']
		+ '" /><br />';
	}else{
		htmls += '<input type="button" onclick="setUnquie()" value="'
		+ lorelang['post_button_unique']
		+ '" /><br />';
	}
	return htmls;
}
function setUnquie(){
	$.cookie('loreswap_unquie', $('#unquie').val());
	$('#htmlUnquie').html(unquieHtml());
}
function delUnquie(){
	$.cookie('loreswap_unquie', null);
	$('#htmlUnquie').html(unquieHtml());
}

function pageHtml(boxname, page, plength, id, listid){
	return '<li>'
		+ (page < 2 ? '<span>&nbsp;' : '<span onclick="' + boxname + '(' + (page - 1) + ',' + id + ',\'' + listid +'\')">' 
		+ lorelang['box_page_prev'])
		+ '</span>'
		+ (plength < limit ? '<span style="float:right;">&nbsp;' 
			: '<span style="float:right;" onclick="' + boxname + '(' + (page + 1) + ',' + id + ',\'' + listid +'\')">'
		+ lorelang['box_page_next'])
		+ '</span></li>';
		
}
function listHtml(funame, adata, page, listid, classname, boxtitle, viewid){
    var htmls='<ul class="' + classname + '"><h3>' + boxtitle + '</h3>';
    $.each(adata, function(key,val){
        htmls += '<li><p onclick="viewDetail(1, ' + val['lore_id'] + ', \'' + viewid + '\')">'
            + val['lore_desc'] 
            + '</p><h5>'
            + lorelang['view_time']
            + val['lore_ctime']
            + '</h5></li>';
    });
    htmls += pageHtml(funame, page, adata.length);
    htmls += '</ul>';
	$(listid).html(htmls);
}
function viewHtml(id, adata, page, listid, classname){
	var htmls='<ul class="' + classname + '"><h3 onclick="' 
		+ (listid.indexOf('boxMan') == -1?'viewRight':'viewMan') 
		+ '()">' 
		+ (listid.indexOf('boxMan') == -1?lorelang['box_right_title']:lorelang['box_man_title'])
		+ '</h3>';
    $.each(adata, function(key,val){
        htmls += '<li' + (!key?' class="box"':'') + '><p>'
            + val['lore_content'] 
            + '</p><h5>'
            + lorelang['view_time']
            + val['lore_ctime']
            + '</h5><span class="lore_reply" id="ls_'
            + val['lore_id']
			+ '_'
            + val['lore_pid']
			+ '">'
			+ lorelang['post_button_reply']
			+ '</span></li>';
    });
    htmls += pageHtml('viewDetail', page, adata.length, id, listid);
    htmls += '</ul>';
	$(listid).html(htmls);
	head.ready(function() {
		$('.lore_reply').bind('click',function(){
			art.dialog({
				title: lorelang['post_box_title'],	
				content: postHtml(this.id),
			});
		});
	});
}
function viewMan(page){
    $.getJSON("get.php", {p:page, t:'man'}, function(data){
		listHtml('viewMan', data, page, '#boxMan', 'box', lorelang['box_man_title'], '#boxRight');
    });
}

function viewRight(page){
    $.getJSON("get.php", {p:page, t:'right'}, function(data){
		listHtml('viewRight', data, page, '#boxRight', 'box', lorelang['box_right_title'], '#boxMan');
    });
}

function viewDetail(page, id, viewSelect){
    $.getJSON("get.php", {t:'detail', i:id, p:page}, function(data){
		viewHtml(id, data, page, viewSelect, 'box');
    });
}

function postData(ths){
	if( $.cookie('unquiefrom') === $("#unquiefrom").val() ){
		$.post("post.php", $("#postFrom").serialize(), function(stuats){
			viewMan();
			var list = art.dialog.list;
			for (var i in list) {
				list[i].close();
			};
		});
	}
}
